package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.order.StartDriveForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import com.atguigu.daijia.order.client.OrderInfoFeignClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerInfoFeignClient customerInfoFeignClient;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public String login(String code) {
        //拿着code进行远程调用,返回用户id
        Result<Long> loginResult = customerInfoFeignClient.login(code);
        //如果返回值失败了,返回错误提示
        Integer codeResult = loginResult.getCode();
        if (codeResult != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        //获取远程调用返回用户id
        Long customerId = loginResult.getData();

        //判断返回用户是否为null,如果为空,返回错误提示
        if (customerId == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //生成token
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        //把用户id 放到redis 中,设置过期时间
        //key:token  value:customerId
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token, customerId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);

        //返回token字符串

        return token;
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        //根据token查询redis
        String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);

        //查询token在redis里面对应的用户id
        if (StringUtils.isEmpty(customerId)){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //根据用户id进行远程调用,得到用户信息
        Result<CustomerLoginVo> customerLoginInfo = customerInfoFeignClient.getCustomerLoginInfo(Long.parseLong(customerId));

        Integer code = customerLoginInfo.getCode();
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        CustomerLoginVo customerLoginVo = customerLoginInfo.getData();
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        return customerLoginVo;
    }

    @Override
    public CustomerLoginVo getCustomerInfo(Long customerId) {

        //根据用户id进行远程调用,得到用户信息
        Result<CustomerLoginVo> customerLoginInfo = customerInfoFeignClient.getCustomerLoginInfo(customerId);

        Integer code = customerLoginInfo.getCode();
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        CustomerLoginVo customerLoginVo = customerLoginInfo.getData();
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        return customerLoginVo;
    }

    @Resource
    OrderInfoFeignClient orderInfoFeignClient;


    @Override
    public Boolean startDriver(StartDriveForm startDriveForm) {

        return orderInfoFeignClient.startDriver(startDriveForm).getData();

    }
}
